package com.llkj.qingnianguoxue.mine;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.jm.chat.pickerimage.utils.StringUtil;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.RecommendMusicAdapter;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.bean.CourseEvaluateBean;
import com.llkj.qingnianguoxue.bean.HotRankingBean;
import com.llkj.qingnianguoxue.bean.MusicLikeBean;
import com.llkj.qingnianguoxue.bean.response.ResponseList;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.music.MusicDetailActivity;
import com.llkj.qingnianguoxue.music.MusicHomeActivity;
import com.llkj.qingnianguoxue.music.RecordMusicActivity;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.ToastUtil;
import com.llkj.qingnianguoxue.util.helper.LoadDataHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyCollectionActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener,
        LoadDataHelper.LoadMoreListener, SwipeRefreshLayout.OnRefreshListener{

    @BindView(R.id.refresh_layout)
    SwipeRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView lvCollections;
    private RecommendMusicAdapter mMusicAdapter;
    private List<CourseEvaluateBean> mMusicData;
    private int totalSize,mPage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_collection);
        setTitle("我的收藏", true, TITLE_TYPE_IMG, R.drawable.icon_back, false, -1, -1);
        ButterKnife.bind(this);

        initView();
        initListener();

        loadWaitDialog();
        onRefresh();
    }

    private void initView(){
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);
        refreshLayout.setColorSchemeResources(R.color.blue3989ff);
        lvCollections.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        mMusicData = new ArrayList<>();
        mMusicAdapter = new RecommendMusicAdapter(R.layout.item_recommend_music,mMusicData);
        mMusicAdapter.setType(1);
        lvCollections.setAdapter(mMusicAdapter);
    }

    private void initListener() {
        refreshLayout.setOnRefreshListener(this);
        mMusicAdapter.setOnLoadMoreListener(this);

        lvCollections.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                CourseEvaluateBean item = mMusicData.get(i);
                if(view.getId()== R.id.btnSing){//演唱
                    if(StringUtil.isEmpty(item.getAccompaniment()) || StringUtil.isEmpty(item.getMp3())
                            || StringUtil.isEmpty(item.getLyric())){
                        ToastUtil.makeLongText(MyCollectionActivity.this,"对不起，音频或歌词文件丢失，请从详情进入试试");
                        return;
                    }
                    RecordMusicActivity.startActivity(MyCollectionActivity.this,item.getId(),item.getSing_img()
                            ,item.getName(),item.getPlay_num(),item.getAccompaniment(),item.getMp3(),item.getLyric(),item.getSing_name());
                }else if(view.getId() == R.id.ivPraise){//喜欢

                }
            }
        });
        lvCollections.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                CourseEvaluateBean item = mMusicData.get(i);
                Bundle bundle  = new Bundle();
                bundle.putString("musicId",item.getCollection_id());
                startActivity(MusicDetailActivity.class,bundle);
            }
        });
    }

    @Override
    public void onRefresh() {
        mPage = 1;
        getCollectionData();
    }

    @Override
    public void onLoadMoreRequested() {
        LoadDataHelper.loadMoreRequest(this, mMusicAdapter, null, lvCollections, totalSize);
    }

    @Override
    public void onLoadMore() {
        mPage++;
        getCollectionData();
    }

    private void getCollectionData(){
        HttpMethod.collection(this,this,String.valueOf(mPage));
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);

        if (method == HttpMethod.HTTP_USER_COLLECTION ) {//8.14我赞收藏
            ResponseList<CourseEvaluateBean> response = GsonUtil.fromJsonList(data,CourseEvaluateBean.class);
            if (response != null) {
                totalSize = Integer.parseInt(response.getNum());
                if (mPage == 1) {
                    mMusicData.clear();
                    LoadDataHelper.refreshComplete(mMusicAdapter, mMusicData, refreshLayout, lvCollections);
                } else {
                    mMusicAdapter.setEnableLoadMore(true);
                    mMusicAdapter.loadMoreComplete();
                }

                mMusicData.addAll(response.getCollection());
                mMusicAdapter.notifyDataSetChanged();
            }
            if(mMusicData.size()==0){
                ToastUtil.makeLongText(this,getString(R.string.empty_collection_note));
            }
        }
    }

    @Override
    protected void onhttpFailed(String message, int method) {
        if(mMusicData.size()==0){
            ToastUtil.makeLongText(this,getString(R.string.empty_collection_note));
        }
    }
}
